<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Type extends Model
{
	/**
	 * 连接数据表
	 *
	 */
	protected $table = 'type';
	/**
	 * 表明模型是否应该被打上时间戳
	 *
	 */
	public $timestamps = false;
	/**
	 * 指定可填充字段
	 *
	 */
// 	protected $fillable = ['id','pid','name'];

	/**
	 * 格式化方法
	 */
	/**
	 * 找子栏目
	 * @param unknown $data
	 * @param number $id
	 * @param number $lev
	 * @return multitype:unknown
	 */
	
	public function  findTree($data,$id=0,$lev=0)
	{
		static $tree = [];
		foreach($data as $k => $v){
			if($v->pid == $id){
				$v->lev= $lev;
				$tree[] = $v;
				$this->findTree($data,$v->id,$lev+1);
			}
		}
		return $tree;
	}
	
	/*
	 * 格式化取出所有栏目
	*/
	public function findAll()
	{
		$data = self::select('id','name','pid')->get();
		return $this->findTree($data,0,0);
	}
	
	/**
	 * 获取子孙栏目
	 */
	public function fundSon($data,$id=0)
	{
		$son = [];
		foreach($data as $v){
			if($v->pid == $id){
				$son[] = $v;
			}	
		}
		return $son;
	}
}
